home *** CD-ROM | disk | FTP | other *** search
- /* trunc - truncate files
-
- trunc [ options ] file ...
-
- -m USER mail file to USER before truncating or removing
- (done only if the file is not empty).
-
- -b BYTES keep only the last BYTES bytes of the file.
-
- -r remove file (useful with -m option).
-
- -o OWNER change owner of file to OWNER (must be numeric).
-
- -g GROUP change group of file to GROUP (must be numeric).
-
- -c MODE change mode of file to MODE.
-
- */
-
- static char sccsid[] = "@(#) trunc.c 1.2 87/03/10 21:32:16";
-
- #include <fcntl.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
-
- #define BUFSIZE 8*1024
- #define MAXINT 0x7fffffff
-
- /*---------------------------------------------------------------------------*/
-
- void mail_file_to_user(filename, user)
- char *filename, *user;
- {
-
- FILE * fpin, *fpout;
- char cmdline[256];
- int c;
-
- if (!(fpin = fopen(filename, "r"))) return;
- sprintf(cmdline, "/bin/mail %s", user);
- if (!(fpout = popen(cmdline, "w"))) return;
- fprintf(fpout, "To: %s\nSubject: %s\n\n", user, filename);
- while ((c = getc(fpin)) != EOF) putc(c, fpout);
- fclose(fpin);
- pclose(fpout);
- }
-
- /*---------------------------------------------------------------------------*/
-
- void truncate_file(filename, bytes)
- char *filename;
- int bytes;
- {
-
- extern long lseek();
-
- char buffer[BUFSIZE];
- char tempfile[256];
- int fi;
- int fo;
- unsigned i;
-
- sprintf(tempfile, "/tmp/trunc%d", getpid());
-
- fi = open(filename, O_RDONLY);
- fo = creat(tempfile, 0600);
- lseek(fi, (long) -bytes, 2);
- while ((i = read(fi, buffer, BUFSIZE)) > 0)
- write(fo, buffer, i);
- close(fi);
- close(fo);
-
- fi = open(tempfile, O_RDONLY);
- fo = creat(filename, 0666);
- while ((i = read(fi, buffer, BUFSIZE)) > 0)
- write(fo, buffer, i);
- close(fi);
- close(fo);
-
- unlink(tempfile);
- }
-
- /*---------------------------------------------------------------------------*/
-
- main(argc, argv)
- int argc;
- char **argv;
- {
-
- extern char *optarg;
- extern int optind;
- extern long strtol();
- extern void exit();
-
- char *filename;
- char *user = 0;
- int bytes = MAXINT;
- int c;
- int errflag = 0;
- int group;
- int mode;
- int owner;
- int remove = 0;
- int set_group = 0;
- int set_mode = 0;
- int set_owner = 0;
- struct stat statbuf;
-
- while ((c = getopt(argc, argv, "m:b:ro:g:c:")) != EOF)
- switch (c) {
- case 'm':
- user = optarg;
- break;
- case 'b':
- bytes = atoi(optarg);
- break;
- case 'r':
- remove = 1;
- break;
- case 'o':
- owner = atoi(optarg);
- set_owner = 1;
- break;
- case 'g':
- group = atoi(optarg);
- set_group = 1;
- break;
- case 'c':
- mode = strtol(optarg, 0, 8);
- set_mode = 1;
- break;
- case '?':
- errflag = 1;
- break;
- }
-
- if (errflag) {
- fprintf(stderr, "usage: trunc [-m user] [-b bytes] [-r] [-o owner] [-g group] [-c mode] file ...\n");
- return 2;
- }
-
- for (; optind < argc; optind++) {
- filename = argv[optind];
-
- if (stat(filename, &statbuf)) continue;
- if (set_group && !set_owner) owner = statbuf.st_uid;
- if (set_owner && !set_group) group = statbuf.st_gid;
-
- if (user && statbuf.st_size) mail_file_to_user(filename, user);
-
- if (remove) {
- unlink(filename);
- continue;
- }
- if (bytes <= 0)
- close(creat(filename, 0644));
- else if (bytes < statbuf.st_size)
- truncate_file(filename, bytes);
-
- if (set_mode) chmod(filename, mode);
- if (set_owner || set_group) chown(filename, owner, group);
- }
- exit(0);
- return 0;
- }
-